#include <iostream>
#include <string.h>
using namespace std;
class DataBase{
private:
  class Students{
  public:
    char ID[8];
    int C;
    int M;
    int E;
    int A;
    int rkC;
    int rkM;
    int rkE;
    int rkA;
  };
  Students STU[2005];
  int StuNum;
public:
  DataBase(int _StuNum){
    StuNum=_StuNum;
  }
  void SetInfo();
  void QSort(int Type);//Type:1 means C,2 means M,3 means E and 4 means A
  void Correct(int Type);//Take 'Same Score' as consider
  void Register(int Type);//Register the rank of each type
  void Check();//Check the best rank
  void quick_sort(int left,int right,int Type);
  int partition(int left,int right,int Type);
};
void DataBase::SetInfo(){
  int sum;
  for(int i=0;i<StuNum;i++){
    scanf("%s%d%d%d",STU[i].ID,&STU[i].C,&STU[i].M,&STU[i].E);
    sum=STU[i].C+STU[i].M+STU[i].E;
    if(sum%3<=1){
      STU[i].A=sum/3;
    }else{
      STU[i].A=sum/3+1;
    }
  }
}
void DataBase::QSort(int Type){
  int right=StuNum;
  int left=0;
  quick_sort(left,right-1,Type);
}
void DataBase::quick_sort(int left,int right,int Type){
  if(left<right){
    int p=partition(left,right,Type);
    quick_sort(left,p,Type);
    quick_sort(p+1,right,Type);
  }
}
int DataBase::partition(int left,int right,int Type){
  int start=left;
  Students pivot=STU[left];
  while(left<right){
    if(Type==1){
	  while(STU[right].C<=pivot.C&&right>0&&right>left)
	  right--;
	  while(STU[left].C>=pivot.C&&left<StuNum-1&&left<right)
	  left++;
    }
    else if(Type==2){
	  while(STU[right].M<=pivot.M&&right>0&&right>left)
	  right--;
	  while(STU[left].M>=pivot.M&&left<StuNum-1&&left<right)
	  left++;
	}
    else if(Type==3){
	  while(STU[right].E<=pivot.E&&right>0&&right>left)
	  right--;
	  while(STU[left].E>=pivot.E&&left<StuNum-1&&left<right)
	  left++;
    }
    else if(Type==4){
	  while(STU[right].A<=pivot.A&&right>0&&right>left)
	  right--;
	  while(STU[left].A>=pivot.A&&left<StuNum-1&&left<right)
	  left++;
    }
    if(right>left){
      Students tmp=STU[left];
      STU[left]=STU[right];
      STU[right]=tmp;
      right--;
      }	
  }
  STU[start]=STU[left];
  STU[left]=pivot;
  return left;
}
void DataBase::Register(int Type){
  for(int i=0;i<StuNum;i++){
    if(Type==1){
      STU[i].rkC=i+1;
    }
    if(Type==2){
      STU[i].rkM=i+1;
    }
    if(Type==3){
      STU[i].rkE=i+1;
    }
    if(Type==4){
      STU[i].rkA=i+1;
    }
  }
}
void DataBase::Correct(int Type){
  for(int i=1;i<StuNum;i++){
    if(Type==1){
      if(STU[i].C==STU[i-1].C){
        STU[i].rkC=STU[i-1].rkC;
      }
    }
    if(Type==2){
      if(STU[i].M==STU[i-1].M){
        STU[i].rkM=STU[i-1].rkM;
      }
    }
    if(Type==3){
      if(STU[i].E==STU[i-1].E){
        STU[i].rkE=STU[i-1].rkE;
      }
    }
    if(Type==4){
      if(STU[i].A==STU[i-1].A){
        STU[i].rkA=STU[i-1].rkA;
      }
    }
  }
}
void DataBase::Check(){
  char _ID[8];
  int rks[5];
  int bstrk;
  int bstsub;
  int i;
  int flag=0;
  int correctnum;
  scanf("%s",_ID);
  for(i=0;i<StuNum;i++){
    correctnum=0;
    for(int j=0;j<6;j++){
      if(_ID[j]==STU[i].ID[j]){
        correctnum++;
      }
    }
    if(correctnum==6){
      break;
    }
  }
  if(i==StuNum){//Not Found
    cout<<"N/A"<<endl;
  }else{
    rks[1]=STU[i].rkA;
    rks[2]=STU[i].rkC;
    rks[3]=STU[i].rkM;
    rks[4]=STU[i].rkE;
    bstrk=rks[1];
    bstsub=1;
    for(i=1;i<=4;i++){
      if(rks[i]<bstrk){
        bstrk=rks[i];
        bstsub=i;
      }
    }
    switch(bstsub){
    case 1:
      cout<<bstrk<<" "<<"A"<<endl;
      break;
    case 2:
      cout<<bstrk<<" "<<"C"<<endl;
      break;
    case 3:
      cout<<bstrk<<" "<<"M"<<endl;
      break;
    case 4:
      cout<<bstrk<<" "<<"E"<<endl;
      break;
    }
  }
}
int main(){
  int _StuNum,CheckNum;
  cin>>_StuNum>>CheckNum;
  DataBase DB(_StuNum);
  DB.SetInfo();
  for(int _type=1;_type<=4;_type++){
    DB.QSort(_type);
    DB.Register(_type);
    DB.Correct(_type);
  }
  for(int i=0;i<CheckNum;i++){
    DB.Check();
  }
}
